package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.proof.wallet.subview

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.bumptech.glide.Glide
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import org.json.JSONObject

interface IProofCameroonWalletDelDialogListener {
    fun onClick(walletId: String)
}

class ProofCameroonWalletDelDialog(var onItemClick: IProofCameroonWalletDelDialogListener) :
    DialogFragment() {

    private lateinit var confirm: TextView
    private lateinit var cancel: TextView
    private lateinit var close: ImageView
    private lateinit var type: TextView
    private lateinit var account: TextView
    private lateinit var icon: ImageView

    private var walletInfo: JSONObject? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(R.layout.view_cameroon_proof_wallet_del_dialog, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        confirm = view.findViewById(R.id.confirm)
        cancel = view.findViewById(R.id.cancel)
        close = view.findViewById(R.id.close)

        icon = view.findViewById(R.id.icon)
        type = view.findViewById(R.id.type)
        account = view.findViewById(R.id.account)

        initializeView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        type.text = this.walletInfo?.optString("walletDesc")
        account.text = this.walletInfo?.optString("walletMobile")

        val logo = this.walletInfo?.optString("walletLogo") ?: ""
        if (logo.isNotBlank()) {
            icon.visibility = View.VISIBLE
            Glide.with(icon).load(logo).into(icon)
        } else {
            icon.visibility = View.GONE
        }
    }

    private fun initializeView() {
        confirm.setOnClickListener {
            val walletId = this.walletInfo?.optString("id")
            if (walletId != null) {
                onItemClick.onClick(walletId)
            }
            dismiss()
        }

        cancel.setOnClickListener {
            dismiss()
        }

        close.setOnClickListener {
            dismiss()
        }
    }

    fun setWalletInfo(jsonObject: JSONObject?) {
        this.walletInfo = jsonObject
    }
}